home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15120 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  179 lines

  1. Path: news.uni-mannheim.de!news
  2. From: luebbeke@zew.de (Michael Luebbeke)
  3. Newsgroups: comp.lang.tcl,comp.unix.programmer,comp.unix.questions,comp.unix.shell,comp.lang.awk,comp.lang.c,comp.lang.perl.misc,comp.programming,comp.software-eng
  4. Subject: Re: Appropriate Tools and Approach for Kill Application
  5. Date: Wed, 17 Apr 1996 19:51:41 GMT
  6. Organization: ZEW Zentrum f. Europ. Wirtsch.-Forschung
  7. Message-ID: <4l2idm$j80@trumpet.uni-mannheim.de>
  8. References: <Pine.SOL.3.91.960417003525.18392A-100000@sun22>
  9. Reply-To: luebbeke@zew.de
  10. NNTP-Posting-Host: mlu.zew.de
  11. X-Newsreader: Forte Free Agent 1.0.82
  12.  
  13. mjw@pobox.com wrote:
  14.  
  15.  
  16.  
  17. >i.e.
  18.  
  19. >kill -n x*
  20.  
  21. >would kill xterm, xrn, xmosaic, etc.
  22.  
  23. Got smthng, quick&dirty programmed, but maybe a start for you:
  24.  
  25. Its from my early programming days, so don┤t wonder ┤bout
  26. root hacking without the use of popular modules like getopt.....
  27. its always spitting out, that processes weren`t killed,
  28. but they are, just ognore it.
  29.  
  30. If anyone is interested, I will do some effort to nicen this stuff up.
  31. email me......
  32.  
  33. -----------------------8<------------------------------>8-----------------------------------------------------
  34. #!/usr/bin/perl
  35. # perl script to kill a spcific program
  36. #
  37.  
  38. $query = 0;
  39. $mode = -1;
  40. $verbose = 0;
  41. $pattern = "";
  42.  
  43.  
  44. for ($in = 0; $in < @ARGV; $in++)
  45. {
  46.  if ($ARGV[$in] =~ /[-\/]v/)
  47.  {
  48.   $verbose = 1;
  49.  }
  50.  if ($ARGV[$in] =~ /[-\/]q/)
  51.  {
  52.   $query= 1;
  53.  }
  54.  if ($ARGV[$in] =~ /[-\/]ma/)
  55.  {
  56.   $mode = 0;
  57.  }
  58.  if ($ARGV[$in] =~ /[-\/]mr/)
  59.  {
  60.   $mode = 1;
  61.  }
  62.  if ($ARGV[$in] =~ /[-\/]mt/)
  63.  {
  64.   $mode = 15;
  65.  }
  66.  if ($ARGV[$in] =~ /[-\/]mk/)
  67.  {
  68.   $mode = 9;
  69.  }
  70.  if ($ARGV[$in] =~ /[-\/][Pp]/)
  71.  {
  72.   $pattern = $ARGV[$in+1];
  73.   if ($verbose == 1)
  74.   {
  75.    print "Looking for pattern: <$pattern>\n";
  76.   }
  77.  } 
  78.  
  79. if ((@ARGV == 0) || (mode == -1) || ($pattern eq ""))
  80. {
  81.  print "KILL: killing processes\nusage: kill [-q] [-v] -m[r|t|k|a] -p
  82. pattern\n";
  83.  print "\t\t-m : r=restart(SIGHUP) t=terminate(SIGTERM)
  84. k=kill(SIGKILL) a = try all\n";
  85.  print "\t\t-q=query\n\t\t-v=verbose\n ";
  86.  exit;
  87. }
  88.  
  89. open(PS, "ps -ax |");
  90.  
  91. while (<PS>)
  92. {
  93.  chop($_);
  94.  $_ =~ s/^\s*//;
  95.  $_ =~ s/\s*$//;
  96.  $_ =~ s/ +/,/o; 
  97.  $_ =~ s/ +/,/o; 
  98.  $_ =~ s/ +/,/o; 
  99.  $_ =~ s/ +/,/o; 
  100.  @procs = ($pid, $tty, $t, $time, $name) = split(/,/, $_);
  101.  &kill_pattern($pattern, $pid, $name, $time, $tty);
  102. }
  103.  
  104. sub kill_pattern
  105. {
  106.  ($pattern, $pid, $name, $time, $tty) = @_;
  107.  
  108.  if ($$ eq $pid)
  109.  {
  110.   return;
  111.  }
  112.   
  113.  if ($name =~ /$pattern/)
  114.  {
  115.   if (($query ==1) || ($verbose == 1))
  116.   {
  117.    print "Kill <$name> PID <$pid> Time <$time> TTY <$tty> ";
  118.   }
  119.   if ($query == 1)
  120.   { 
  121.    print " Should I kill it? ";
  122.    if (<STDIN> =~ /[NnQq]/)
  123.    {
  124.     return;
  125.    }
  126.   }
  127.   
  128.   if ($mode == 0)
  129.   {
  130.    print "Sending HUP ...";
  131.    if ((kill(1 , $pid) == 0) || (&test_kill($pid) == 0))
  132.    {
  133.     print "Sending TERM ...";
  134.     if ((kill(15 , $pid) == 0) || (&test_kill($pid) == 0))
  135.     {
  136.      print "Sending KILL ...";
  137.      if ((kill(9 , $pid) == 0) || (&test_kill($pid) == 0))
  138.      {
  139.       print " can't kill it.\n";
  140.       return;
  141.      }
  142.     }
  143.     print " ... OK.\n";
  144.    }
  145.   }
  146.   else
  147.   {
  148.    print "Sending Signal <$mode> ...";
  149.    if ((kill($mode, $pid) == 0) || (&test_kill($pid) == 0))
  150.    { 
  151.     print " ... no success.\n";
  152.     return;
  153.    }
  154.    print " ... OK.\n";
  155.   }
  156.  } 
  157.  return;
  158. }
  159.  
  160. sub test_kill
  161. {
  162.  $p = $_[0];
  163.  open(TEST, "ps -p $p |");
  164.  @t = <TEST>;
  165.  close(TEST);
  166.  
  167.  foreach $i (@t)
  168.  {
  169.   if ($i =~ /^\s*$p/)
  170.   {
  171.    return 0;
  172.   }
  173.  }
  174.  return 1;
  175. }
  176.  
  177.  
  178.